class Solution
{
public:
    int minimumDeletions(string s)
    {
        int n = s.size();
        vector<int> frontBCount(n, 0);
        vector<int> backACount(n, 0);
        for (int i = 1; i < n; ++i)
        {
            if (s[i - 1] == 'b')
            {
                frontBCount[i] = frontBCount[i - 1] + 1;
            }
            else
            {
                frontBCount[i] = frontBCount[i - 1];
            }
        }
        for (int i = n - 2; i >= 0; --i)
        {
            if (s[i + 1] == 'a')
            {
                backACount[i] = backACount[i + 1] + 1;
            }
            else
            {
                backACount[i] = backACount[i + 1];
            }
        }

        int minDel = INT_MAX;
        for (int i = 0; i < n; ++i)
        {
            minDel = min(minDel, frontBCount[i] + backACount[i]);
        }
        return minDel;
    }
};